package com.easylinkin.linkappapi.bim.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.easylinkin.core.models.Operator;
import com.easylinkin.linkappapi.bim.entity.BimServerConfig;
import com.easylinkin.linkappapi.bim.mapper.BimServerConfigMapper;
import com.easylinkin.linkappapi.bim.dto.BindingPlanDto;
import com.easylinkin.linkappapi.bim.entity.BimProjectImportRecord;
import com.easylinkin.linkappapi.bim.entity.BimTask;
import com.easylinkin.linkappapi.bim.service.BimProjectImportRecordService;
import com.easylinkin.linkappapi.bim.service.BimTaskService;
import com.easylinkin.linkappapi.bim.vo.BimProjectImportRecordVo;
import com.easylinkin.linkappapi.common.model.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 任务计划管理
 *
 * @author pwqoc
 * @since 2024-05-24 15:02:21
 */
@RestController
@RequestMapping("bim/schedule")
@Api(tags = "任务计划管理相关接口")
public class BimScheduleController extends BaseController {
    public static final String DATE_FORMAT = "yyyy-MM-dd";


    @Resource
    private BimProjectImportRecordService projectImportRecordService;
    @Resource
    private BimTaskService taskService;
    @Resource
    private BimServerConfigMapper bimServerConfigMapper;

    @ApiOperation(value = "导入", notes = "导入")
    @PostMapping("import")
    public RestMessage importProject(@RequestParam(value = "file") MultipartFile file) {
        projectImportRecordService.importProject(file, getOperator());
        return RestBuilders.successBuilder().data("导入成功").build();
    }



    @ApiOperation(value = "导入记录下拉列表", notes = "导入记录下拉列表")
    @GetMapping("list")
    public RestMessage list() {
        return RestBuilders.successBuilder().data(BeanUtil.copyToList(projectImportRecordService.list(
                new LambdaQueryWrapper<BimProjectImportRecord>().eq(BimProjectImportRecord::getTenantId, getOperator().getTenantId())),
                BimProjectImportRecordVo.class)).build();
    }



    @ApiOperation(value = "任务计划tree", notes = "任务计划tree")
    @GetMapping("tree")
    public RestMessage tree(@RequestParam(BimTask.Fields.importRecordId) String importRecordId,
                            @RequestParam(value = BimTask.Fields.taskName, required = false) String taskName) {
        return RestBuilders.successBuilder().data(taskService.tree(importRecordId, taskName, getOperator().getTenantId())).build();
    }

    @ApiOperation(value = "绑定计划", notes = "绑定计划")
    @PostMapping("bindingPlan")
    public RestMessage bindingPlan(@RequestBody BindingPlanDto dto) {
        taskService.bindingPlan(dto, getOperator().getTenantId());
        return RestBuilders.successBuilder().data("绑定成功").build();
    }

    @ApiOperation(value = "绑定关系", notes = "绑定关系")
    @GetMapping("bindingRelationship")
    public RestMessage bindingRelationship(@RequestParam(BimTask.Fields.importRecordId) String importRecordId) {
        return RestBuilders.successBuilder().data(taskService.bindingRelationship(importRecordId, getOperator().getTenantId())).build();
    }

    @ApiOperation(value = "文件ViewToken", notes = "文件ViewToken")
    @GetMapping("getBimfaceViewToken")
    public RestMessage getBimfaceViewToken() {
        return RestBuilders.successBuilder().data(taskService.getBimfaceViewToken(getOperator())).build();
    }

    @ApiOperation(value = "获取Bimface配置", notes = "获取Bimface配置")
    @GetMapping("getBimServerConfig")
    public RestMessage getBimServerConfig() {
        return RestBuilders.successBuilder().data(bimServerConfigMapper.selectOne(new LambdaQueryWrapper<BimServerConfig>()
                .eq(BimServerConfig::getTenantId, getOperator().getTenantId()))).build();
    }

    @ApiOperation(value = "保存Bimface配置", notes = "保存Bimface配置")
    @PostMapping("saveBimServerConfig")
    public RestMessage saveBimServerConfig(@Validated @RequestBody BimServerConfig bimServerConfig) {
        Operator operator = getOperator();
        BimServerConfig serverConfig = new BimServerConfig();
        serverConfig.setAppKey(bimServerConfig.getAppKey());
        serverConfig.setAppSecret(bimServerConfig.getAppSecret());
        serverConfig.setFileId(bimServerConfig.getFileId());

        if (StrUtil.isBlank(bimServerConfig.getId())) {
            serverConfig.setId(IdUtil.getSnowflake().nextIdStr());
            serverConfig.setTenantId(operator.getTenantId());
            serverConfig.setCreatorId(operator.getUserId());
            serverConfig.setCreator(operator.getNickname());
            serverConfig.setCreateTime(new Date());
            if (!SqlHelper.retBool(bimServerConfigMapper.insert(serverConfig))) {
                return RestBuilders.errorBuilder().data("保存失败").build();
            }
        }else {
            serverConfig.setId(bimServerConfig.getId());
            serverConfig.setModifyId(operator.getUserId());
            serverConfig.setModifier(operator.getNickname());
            serverConfig.setModifyTime(new Date());
            if (!SqlHelper.retBool(bimServerConfigMapper.updateById(serverConfig))) {
                return RestBuilders.errorBuilder().data("保存失败").build();
            }
        }
        return RestBuilders.successBuilder().data("保存成功").build();
    }

}

